package org.jeecg.demo.wx.convert;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;

import org.jeecg.demo.wx.dto.RlTaskSaveDto;
import org.jeecg.demo.wx.dto.RlTaskWebSaveDto;
import org.jeecg.demo.wx.dto.TaskUpdateDto;
import org.jeecg.demo.wx.entity.Meeting;
import org.jeecg.demo.wx.entity.RlSchedule;
import org.jeecg.demo.wx.entity.RlTask;
import org.jeecg.demo.wx.vo.OneTaskVO;
import org.jeecg.demo.wx.vo.QueryTaskVO;
import org.jeecg.demo.wx.vo.UserCompleteVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Date;
import java.util.List;


@Mapper
public interface TaskTransfer {
    TaskTransfer INSTANCE = Mappers.getMapper(TaskTransfer.class);


    /**
     * 列表转字符串
     * @param param
     * @return
     */
/*    @Mappings({
            @Mapping(expression = "java(TaskTransfer.list2String(param.getRecipientId()))", target = "recipientId"),
            @Mapping(expression = "java(TaskTransfer.list2String(param.getRecipientName()))", target = "recipientName"),
    })*/
    RlTask toEntity(RlTaskSaveDto param);
/*
    @Mappings({
            @Mapping(target = "isRepeat", constant = ""),
            @Mapping(target = "isHoliday", constant = ""),
            @Mapping(target = "isWeekend", constant = ""),
            @Mapping(target = "isComplete", constant = "10"),
            @Mapping(target = "state", constant = "20"),
            @Mapping(target = "delFlag", constant = "0")
    })*/
    RlTask toEntity(TaskUpdateDto param);
/*    @Mappings({
            @Mapping(source = "userId", target = "id"),
            @Mapping(source = "userName", target = "name"),
            @Mapping(source = "isComplete", target = "isComplete"),
            @Mapping(target = "isCompleteText", ignore = true)
    })*/
    UserCompleteVO toUserVo(RlSchedule param);

    //
    RlTask toTask(RlTaskWebSaveDto param);

    //
    OneTaskVO toVo(RlTask param);
    /**
     * task转schedule，轮询的
     * @param param
     * @return
     */
 /*   @Mappings({
            @Mapping(source = "taskType", target = "scheduleType"),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "userId", ignore = true),
            @Mapping(target = "userName", ignore = true),
            @Mapping(target = "isComplete", constant = "10"),   // 设置完成-未读
            @Mapping(target = "state", constant = "20"),    // 设置状态-任务
            @Mapping(target = "delFlag", constant = "0"),   // 设置显示-正常
            @Mapping(target = "isOa", constant = "1"),       // 关联OA
            @Mapping(target = "createBy", constant = "OA")  // 设置创建源
    })*/
    RlSchedule toSchedule(RlTask param);


    /**
     * task转schedule，不带oa字段
     * @param param
     * @return
     */
/*    @Mappings({
            @Mapping(source = "taskType", target = "scheduleType"),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "userId", ignore = true),
            @Mapping(target = "userName", ignore = true),
            @Mapping(target = "isComplete", constant = "10"),   // 设置完成-未读
            @Mapping(target = "state", constant = "20"),    // 设置状态-任务
            @Mapping(target = "delFlag", constant = "0"),   // 设置显示-正常
    })*/
    RlSchedule onlySchedule(RlTask param);
    /**
     * oa系统获得数据转换
     * const表示常量不用source，defaultValue要source
     * 转换参数在枚举类查看
     * @param param
     * @return
     */
/*    @Mappings({
            @Mapping(source = "name", target = "title", defaultValue = ""),
            @Mapping(source = "creater", target = "userId", defaultValue = ""),
            @Mapping(source = "descN", target = "content", defaultValue = ""),
            @Mapping(source = "hrmmembers", target = "recipientId", defaultValue = ""),
            @Mapping(expression = "java(TaskTransfer.string2Date(param.getBegindate(), param.getBegintime()))", target = "beginTime"),
            @Mapping(expression = "java(TaskTransfer.string2Date(param.getEnddate(), param.getEndtime()))", target = "endTime"),
            @Mapping(target = "taskType", ignore = true),
            @Mapping(target = "level", constant = "20"),
            @Mapping(target = "isRepeat", constant = "1"),
            @Mapping(target = "isHoliday", constant = "1"),
            @Mapping(target = "isWeekend", constant = "1"),
            @Mapping(target = "isOa", constant = "1"),
            @Mapping(source = "id", target = "oaId"),
            @Mapping(target = "state", constant = "20"),
            @Mapping(target = "delFlag", constant = "0"),
            @Mapping(target = "id", ignore = true),
            @Mapping(target = "createBy", constant = "OA"),
            @Mapping(source = "address", target = "address"),
            @Mapping(target = "isComplete", constant = "10")
    })*/
    RlTask toRlTask(Meeting param);
    /**
     * 查询结果转vo，切割时间
     * @param param
     * @return
     */
    List<QueryTaskVO> toQueryTask(List<RlTask> param);
    QueryTaskVO toQueryTask(RlTask param);

    RlTaskSaveDto updateToSave(TaskUpdateDto param);
    /**
     * 字符串列表转字符串
     * @param param
     * @return
     */
    static String list2String(List<String> param){
        return String.join(",", param);
    }

    /**
     * 数组列表转字符串
     * @param param
     * @return
     */
    static String int2String(List<Integer> param){
        return String.join(",", String.join(",", Convert.toStrArray(param)));
    }

    /**
     * 时间转换，拼接时间
     * @param date
     * @param time
     * @return
     */
    static Date string2Date(String date, String time) {
        // date + " " + time + :00，time不带秒
        String s = String.valueOf(new StringBuilder().append(date).append(" ").append(time).append(":00"));
        Date result = DateUtil.parse(s, "yyyy-MM-dd HH:mm:ss");
        return result;
    }



}
